package com.example.demo

import android.app.Application
import android.util.Log
import com.example.demo.database.DatabaseCallbackManager
import com.example.demo.service.UserProfileService
import com.example.featuresdk.navigation.INavigationMediator
import dagger.hilt.android.HiltAndroidApp
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltAndroidApp
class DemoApplication : Application() {

    companion object{
        const val TAG = "DemoApplication"
    }
//    @Inject @ApplicationContext
//    lateinit var context: Context

    @Inject
    lateinit var databaseCallbackManager: DatabaseCallbackManager

    @Inject
    lateinit var userProfileService: UserProfileService

    @Inject
    lateinit var navigationMediator: INavigationMediator

    override fun onCreate() {
        super.onCreate()

        CoroutineScope(SupervisorJob()).launch {
            databaseCallbackManager.initialize()

            entitleFeatures()
        }
    }

//    private fun getFeatureSDKs(): Map<String,String>{
//
//        val regionFeatures = mutableMapOf<String,String>()
//
//        packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA).apply {
//            metaData.getString("featureSDKs")?.split(";")?.forEach {
//                it.split("=").apply {
//                    regionFeatures[this[0].trim()] = this[1].trim()
//                }
//            }
//        }
//
//        Log.i("DemoApplication - $packageName","$regionFeatures")
//
//        return regionFeatures
//    }

//    private fun initFeatures(){
//        runCatching{
////            val reader = context.assets.open("feature.json").reader()
////            Gson().fromJson(reader,HashMap::class.java)
//            val reader = context.assets.open("app-bootstrap.properties").reader()
//            Properties().apply {
//                load(reader)
//                reader.close()
//            }
//        }.onFailure {
//            Log.e("DemoApplication","environment",it)
//        }.onSuccess { features ->
//            CoroutineScope(SupervisorJob()).launch {
//                NavigationMediator.instance.clear()
//                Log.i("DemoApplication - features", features.toString())
//
//                features["feature-order"].toString().split(";").mapIndexed { index, key ->
//                    NavigationMediator.instance.featureOrder[key] = index
//                }
//
//                userProfileService.getUserFeatures().execute().body()?.data?.forEach {
//                    if(features.containsKey(it)){
//                        Log.i("DemoApplication", "Creating Feature SDK: ${features[it]}")
//                        val featureSDK = Class.forName(features[it] as String).newInstance() as AbstractFeatureSDK
//                        NavigationMediator.instance.register(featureSDK)
//                    }else{
//                        Log.e("DemoApplication", "Feature SDK Not Found:$it")
//                    }
//                }
//            }
//        }
//    }

    private fun entitleFeatures(){
        navigationMediator.features().forEach {
            it.userEntitled = false
        }
        userProfileService.getUserFeatures().execute().body()?.data?.forEach {
            Log.i(TAG,"feature[$it] is entitled to user")
            navigationMediator.feature(it)?.userEntitled = true
        }
    }
}
